//
//  JXGovementViewController.m
//  JXAddressApp
//
//  Created by zhhw_home on 2019/1/6.
//  Copyright © 2019年 zhhw_home. All rights reserved.
//

#import "JXGovementViewController.h"
#import <QMUIKit/UIColor+QMUI.h>
#import "JXNewsTableViewCell.h"
#import "Masonry.h"
#import "EgovaNetworking.h"
#import "JXNewModel.h"
#import "UIImageView+AFNetworking.h"
#import "JXGovementModel.h"
#import "YYModel.h"
#import "RATreeView.h"
#import "JXBaseWKWebViewController.h"

@interface JXGovementViewController ()
<
RATreeViewDataSource,
RATreeViewDelegate
>

@property (nonatomic, strong) RATreeView        *treeView;
@property (nonatomic, strong) NSArray<JXGovementModel*>           *models;

@end

@implementation JXGovementViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self initRootView];
    [self initdt];
    
    if (@available(iOS 11.0, *)) {
        self.treeView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
}

- (void)initRootView
{
    self.view.backgroundColor = [UIColor qmui_colorWithHexString:@"#EAEAEA"];
    
    _treeView = [[RATreeView alloc] initWithFrame:self.view.bounds];
    _treeView.dataSource = self;
    _treeView.delegate = self;
    self.treeView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_treeView];
    
    ((UITableView*)self.treeView.scrollView).tableFooterView = [UIView new];
    
    [self.treeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    
    [_treeView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
}

- (void)initdt
{
    NSString *url = @"http://www.wegis.net/demo//home/baseinfo/getgovfunbyseniorid?seniorID=-1";
    [EgovaNetworking getWithUrl:url cache:NO completed:^(NSInteger code, id data, NSString *msg) {
        NSLog(@"%@", data);
        if (data[@"treeList"]) {
            self.models = [NSArray yy_modelArrayWithClass:[JXGovementModel class] json:data[@"treeList"]];
        }
        
        [self.treeView reloadData];
    }];
}

- (NSInteger)treeView:(RATreeView *)treeView numberOfChildrenOfItem:(nullable id)item
{
    JXGovementModel *model = (JXGovementModel*)item;
    if (item) {
        return model.children.count;
    }
    
    return _models.count;
}

- (id)treeView:(RATreeView *)treeView child:(NSInteger)index ofItem:(id)item
{
    JXGovementModel *model = (JXGovementModel*)item;
    if (item) {
        return model.children[index];
    }
    
    return _models[index];
}

- (nonnull UITableViewCell *)treeView:(nonnull RATreeView *)treeView cellForItem:(nullable id)item
{
    NSInteger level = [self.treeView levelForCellForItem:item];
    JXGovementModel *model = (JXGovementModel*)item;
    UITableViewCell *cell = [treeView dequeueReusableCellWithIdentifier:@"UITableViewCell"];

    NSString *text = model.text;
    NSString *empty = @"    ";
    for (int i=0; i<level; i++) {
        text = [empty stringByAppendingString:text];
    }
    
    cell.textLabel.text = text;
    
    return cell;
}

- (void)treeView:(RATreeView *)treeView didSelectRowForItem:(id)item
{
    JXGovementModel *model = (JXGovementModel*)item;
    
    if (model.children.count == 0) {
        NSString *fileurl = [[NSString alloc] initWithFormat:@"http://www.wegis.net/demo/view/mobile/dist/index.html#/govregion/%@/detail", [model.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ];
        
        JXBaseWKWebViewController *vc = [JXBaseWKWebViewController new];
        vc.webUrl = [NSURL URLWithString:fileurl];
        vc.webVCTitle = model.text;
        vc.hidesBottomBarWhenPushed = YES;
        [self.parentViewController.navigationController pushViewController:vc animated:YES];
        
        [treeView deselectRowForItem:item animated:YES];
    }
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
